import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/up-form.vue"; //弹框form
import FormLyout from '@/components/commonPublic/fromLayout.vue'
import actions from "@/actions";

import {
    saveOutsourcednon, //闭合保存
    getOutsourcednonByFid, //详情
    checkOutsourcednon1, //一级审核
    checkOutsourcednon2, //一级审核


} from "@/api/request.js";
export default {

    data() {
        return {
            activeNames: ["0"],
            loading:false,
            handNum: false, //闭合申请单
            firstNum: false, //一级审核
            secondNum: false, //二级审核
            visible: "",
            title: null,
            isLock: false,
            btnStatus: [],
            positionObj: [],
            dialogHeight: null,
            activeIndex: "1",
            baseFormObj: formJs.baseFormObj, //基础信息
            baseFormData: {},
            closeFormObj: formJs.closeFormObj, //闭合
            closeFileurllist: [], //附件
            closeFormData: { //闭合 closeFormObj
                fId: "", //必传
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                recordfiles: [], //删除文件id集合
                fNonresult: "", //不合格参数及结果
                fProcessing: "", //处理方式
                fProcessusername: "", //处理人姓名
                fProcessuserid: "", //处理人id
                fProcessdates: this.common.day(0), //处理日期
                fProcessfileurl: "" //闭合申请单附件
            },
            FauditFormObj: formJs.Faudit, //一级审核
            FauditFileurllist: [], //附件
            FauditFormData: { //闭合 closeFormObj
                fId: "", //必传
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                recordfiles: [], //删除文件id集合
                fCheckresult1: "", //一级审核结果
                fCheckopinion1: "", //一级审核意见
                fCheckuserid1: "", //一级审核人id
                fCheckusername1: "", //一级审核人姓名
                fCheckdate1s: "", //一级审核日期
                fCheckfileurl1: "" //一级审核附件
            },
            SauditFormObj: formJs.Saudit, //二级审核
            SauditFileurllist: [],
            SauditFormData: { //闭合 closeFormObj
                fId: "", //必传
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                recordfiles: [], //删除文件id集合
                fCheckresult2: "", //一级审核结果
                fCheckopinion2: "", //一级审核意见
                fCheckuserid2: "", //一级审核人id
                fCheckusername2: "", //一级审核人姓名
                fCheckdate2s: "", //一级审核日期
                fCheckfileurl2: "" //一级审核附件
            },
            btnShow: true,
            currentDetail: null,


            fCompanynames: null,
            fCompanyids: null,
            modelTypes: null,
            primaryInfo:null,


        };
    },
    components: {
        publicForm,
        FormLyout
    },
    
   
    mounted() {
        this.primaryInfo = JSON.parse(sessionStorage.getItem("GxEmbedded_primaryInfo"))
        this.currentDetail=this.primaryInfo.data
        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.userInfo = prev.currentUser;
        }, true);
        this.btnShow = true
        if (this.primaryInfo.status == 1) { //闭合
            this.getDetail({
                fId: this.primaryInfo.data.fId
            })
           
        } else if (this.primaryInfo.status == 7) { //查看
            this.getDetail({
                fId: this.primaryInfo.data.fId
            })
            this.btnShow = false
            this.handNum = true; //闭合申请单
            this.firstNum = true; //一级审核
            this.secondNum = true; //二级审核
            // 闭合
            this.closeFormObj.map((item) => {
                item.disabled = true;
            });
            // 闭合
            // 一级审核
            this.FauditFormObj.map((item) => {
                item.disabled = true;
            });
            // 一级审核
            //  二级闭合
            this.SauditFormObj.map((item) => {
                item.disabled = true;
            });
            this.closeFormObj[3].uploadSelect = false
            this.FauditFormObj[4].uploadSelect = false
            this.SauditFormObj[4].uploadSelect = false
            //  二级闭合
        }
        this.getHeight();
    },
    methods: {
        // 折叠面板
        changeCollapse(e) {
            console.log(this.activeNames, e);
        },
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            if (b == "fManufacturername") { //生产厂家
                this.closeFormData.fManufacturername = data.label; //生产厂家
                this.closeFormData.fManufacturerid = data.value //生产厂家id
            } else if (b == "fSuppliername") { //供应商名称
                this.closeFormData.fSuppliername = data.label; //供应商
                this.closeFormData.fSupplierid = data.value //供应商id
            } else if (b == 'fSpecification') {
                this.closeFormData.fSpecification = data.value //供应商id
            }
        },
        edit() {
            this.closeFormData = this.$options.data.call(this).closeFormData;
        },
        // 刪除文件
        delteFile(file, status) {
            var obj;
            var arr;
            if (status == 0) {
                obj = this.closeFormData;
                arr = this.closeFileurllist;
            } else if (status == 1) {
                obj = this.FauditFormData;
                arr = this.FauditFileurllist;
            } else if (status == 2) {
                obj = this.SauditFormData;
                arr = this.SauditFileurllist;
            }
            for (let index = 0; index < arr.length; index++) {
                if (file.uid == arr[index].uid) {
                    if (arr[index].atta == undefined) {
                        obj.recordfiles.push({
                            fId: arr[index].fId,
                        });
                    } else {
                        obj.recordfiles.push({
                            fId: arr[index].atta.fId,
                        });
                    }
                    this.closeFileurllist.splice(index, 1);
                }
            }
        },
        // 详情
        getDetail(obj) {
            this.loading=true;
            getOutsourcednonByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading=false;
                    var data = res.data.yjQmpClOutsourcednon
                 
                    this.baseFormData = data;
                    this.closeFormData.fId = this.currentDetail.fId //按钮 
                    this.FauditFormData.fId = this.currentDetail.fId //按钮 
                    this.SauditFormData.fId = this.currentDetail.fId //按钮 

                    this.closeFormData.fNonresult = data.fNonresult
                    this.closeFormData.fProcessfileurl = data.fProcessfileurl; //  闭合
                    this.FauditFormData.fCheckfileurl1 = data.fCheckfileurl1; //  一级审核
                    this.SauditFormData.fCheckfileurl2 = data.fCheckfileurl2; //  一级审核
                    if (this.currentDetail.fStatus == '待处理') {

                        this.handNum = true; //闭合申请单
                        this.firstNum = false; //一级审核
                        this.secondNum = false; //二级审核
                        this.activeNames = ["0"];
                        if (this.primaryInfo.status == 7) {
                            this.handNum = false; //一级审核
                        } else {
                            this.handNum = true; //一级审核

                        }
                        // 闭合
                        this.closeFormObj.map((item) => {
                            item.disabled = true;
                        });
                        this.closeFormObj[0].disabled = false;
                        this.closeFormObj[3].disabled = false;
                        // 闭合
                        // 一级审核
                        this.FauditFormObj.map((item) => {
                            item.disabled = true;
                        });
                        // 一级审核
                        //  二级闭合
                        this.SauditFormObj.map((item) => {
                            item.disabled = true;
                        });
                        //  二级闭合
                        this.closeFormObj[3].uploadSelect = true
                        this.FauditFormObj[4].uploadSelect = false
                        this.SauditFormObj[4].uploadSelect = false
                    }else if (this.currentDetail.fStatus == '一级审核未通过') {

                        this.handNum = true; //闭合申请单
                        this.firstNum = false; //一级审核
                        this.secondNum = false; //二级审核
                        this.activeNames = ["0"];
                        if (this.primaryInfo.status == 7) {
                            this.handNum = false; //一级审核
                        } else {
                            this.handNum = true; //一级审核

                        }
                        // 闭合
                        this.closeFormObj.map((item) => {
                            item.disabled = true;
                        });
                        this.closeFormObj[0].disabled = false;
                        this.closeFormObj[3].disabled = false;
                        // 闭合
                        // 一级审核
                        this.FauditFormObj.map((item) => {
                            item.disabled = true;
                        });
                        // 一级审核
                        //  二级闭合
                        this.SauditFormObj.map((item) => {
                            item.disabled = true;
                        });
                        //  二级闭合
                        this.closeFormObj[3].uploadSelect = true
                        this.FauditFormObj[4].uploadSelect = false
                        this.SauditFormObj[4].uploadSelect = false
                    }else if (this.currentDetail.fStatus == '一级待审核') {
                        this.handNum = true; //闭合申请单
                        this.secondNum = false; //二级审核
                        // 闭合
                        this.closeFormObj.map((item) => {
                            item.disabled = true;
                        });
                        // 闭合
                        this.firstNum = true; //一级审核
                            // 一级审核
                            this.FauditFormObj.map((item) => {
                                item.disabled = false;
                            });
                            this.FauditFormObj[2].disabled = true;
                            this.FauditFormObj[3].disabled = true;
                            // 一级审核
                            this.activeNames = ["0", "1"];
                        // if (this.modelTypes == 1) { //已办
                        //     this.firstNum = false; //一级审核
                        //     this.FauditFormObj.map((item) => {
                        //         item.disabled = true
                        //     });
                        //     this.activeNames = ["0"];

                        // } else if (this.modelTypes == 0) { //待办
                        //     this.firstNum = true; //一级审核
                        //     // 一级审核
                        //     this.FauditFormObj.map((item) => {
                        //         item.disabled = false;
                        //     });
                        //     this.FauditFormObj[2].disabled = true;
                        //     this.FauditFormObj[3].disabled = true;
                        //     // 一级审核
                        //     this.activeNames = ["0", "1"];

                        // }
                        if (this.primaryInfo.status == 7) {
                            this.firstNum = false; //一级审核
                        } else {
                            this.firstNum = true; //一级审核
                        }

                        //  二级闭合
                        this.SauditFormObj.map((item) => {
                            item.disabled = true;
                        });
                        this.closeFormObj[3].uploadSelect = false
                        this.FauditFormObj[4].uploadSelect = true
                        this.SauditFormObj[4].uploadSelect = false
                        //  二级闭合
                    }else if (this.currentDetail.fStatus == '二级审核未通过') {
                        this.handNum = true; //闭合申请单

                        this.secondNum = false; //二级审核

                        // 闭合
                        this.closeFormObj.map((item) => {
                            item.disabled = true;
                        });

                        // 闭合
                        this.firstNum = true; //一级审核
                        // 一级审核
                        this.FauditFormObj.map((item) => {
                            item.disabled = false;
                        });
                        this.FauditFormObj[2].disabled = true;
                        this.FauditFormObj[3].disabled = true;
                        // 一级审核
                        this.activeNames = ["0", "1"];
                        // if (this.modelTypes == 1) { //已办
                        //     this.firstNum = false; //一级审核
                        //     this.FauditFormObj.map((item) => {
                        //         item.disabled = true
                        //     });
                        //     this.activeNames = ["0"];

                        // } else if (this.modelTypes == 0) { //待办
                        //     this.firstNum = true; //一级审核
                        //     // 一级审核
                        //     this.FauditFormObj.map((item) => {
                        //         item.disabled = false;
                        //     });
                        //     this.FauditFormObj[2].disabled = true;
                        //     this.FauditFormObj[3].disabled = true;
                        //     // 一级审核
                        //     this.activeNames = ["0", "1"];

                        // }
                        if (this.primaryInfo.status == 7) {
                            this.firstNum = false; //一级审核
                        } else {
                            this.firstNum = true; //一级审核
                        }

                        //  二级闭合
                        this.SauditFormObj.map((item) => {
                            item.disabled = true;
                        });
                        this.closeFormObj[3].uploadSelect = false
                        this.FauditFormObj[4].uploadSelect = true
                        this.SauditFormObj[4].uploadSelect = false
                        //  二级闭合
                    }  else if (this.currentDetail.fStatus == '二级待审核') {
                        this.handNum = true; //闭合申请单
                        this.firstNum = true; //一级审核
                        this.secondNum = true; //二级审核



                        // 闭合
                        this.closeFormObj.map((item) => {
                            item.disabled = true;
                        });
                        // 闭合
                        // 一级审核
                        this.FauditFormObj.map((item) => {
                            item.disabled = true;
                        });
                        if (this.primaryInfo.status  == 7) {
                            this.secondNum = false; //一级审核
                            this.activeNames = ["0", "1"];
                        } else {
                            this.secondNum = true; //一级审核
                            this.activeNames = ["0", "1", "2"];
                        }
                        // 一级审核
                        //  二级闭合
                        this.SauditFormObj.map((item) => {
                            item.disabled = false;
                        });
                        this.SauditFormObj[2].disabled = true;
                        this.SauditFormObj[3].disabled = true;
                        this.closeFormObj[3].uploadSelect = false
                        this.FauditFormObj[4].uploadSelect = false
                        this.SauditFormObj[4].uploadSelect = true
                        //  二级闭合
                    } else if (this.currentDetail.fStatus == '已完成') {
                        this.handNum = true; //闭合申请单
                        this.firstNum = true; //一级审核
                        this.secondNum = true; //二级审核
                        // 闭合
                        this.closeFormObj.map((item) => {
                            item.disabled = true;
                        });
                        // 闭合
                        // 一级审核
                        this.FauditFormObj.map((item) => {
                            item.disabled = true;
                        });
                        // 一级审核
                        //  二级闭合
                        this.SauditFormObj.map((item) => {
                            item.disabled = true;
                        });
                        this.closeFormObj[3].uploadSelect = false
                        this.FauditFormObj[4].uploadSelect = false
                        this.SauditFormObj[4].uploadSelect = false
                        //  二级闭合
                    }
                    // 闭合
                    if (data.fProcessdates == null || data.fProcessdates == "") {
                        this.closeFormData.fProcessdates = this.common.day(0)
                        this.closeFormData.fProcessusername = this.userInfo.userName; //处理人
                        this.closeFormData.fProcessuserid = this.userInfo.id; //处理人id

                    } else {
                        this.closeFormData = data
                        this.closeFormData.recordfiles = []
                        this.closeFileurllist = res.data.fProcessfileurllist
                    }

                    if (data.fCheckdate1s == null || data.fCheckdate1s == "") {
                        this.FauditFormData.fCheckdate1s = this.common.day(0)
                        this.FauditFormData.fCheckusername1 = this.userInfo.userName; //一级审核人姓名
                        this.FauditFormData.fCheckuserid1 = this.userInfo.id; //一级审核人id
                    } else {
                        this.FauditFormData = data
                        this.FauditFormData.recordfiles = []
                        this.FauditFileurllist = res.data.fCheckfileurl1list
                    }
                    // 二级审核

                    if (data.fCheckdate2s == null || data.fCheckdate2s == "") {
                        this.SauditFormData.fCheckdate2s = this.common.day(0)
                        this.SauditFormData.fCheckusername2 = this.userInfo.userName; //一级审核人姓名
                        this.SauditFormData.fCheckuserid2 = this.userInfo.id; //一级审核人id
                    } else {
                        this.SauditFormData = data

                        this.SauditFormData.recordfiles = []
                        this.SauditFileurllist = res.data.fCheckfileurl2list
                    }
                }else {
                    this.loading=false;
                    this.$message.error({
                        content: res.msg,
                        duration: 1000
                    })
                }
                if (this.primaryInfo.status == 7) {
                    this.btnShow = false;
                } else {
                    this.btnShow = true;
                }
            });
        },
        yj_saveForm() {
         
            this.closeFormData.userId = this.userInfo.id; //当前登录人id
            this.closeFormData.userName = this.userInfo.userName; //当前登录人名称
            // 一级审核
            this.FauditFormData.userId = this.userInfo.id; //当前登录人id
            this.FauditFormData.userName = this.userInfo.userName; //当前登录人名称
            // 二级审核

            this.SauditFormData.userId = this.userInfo.id; //当前登录人id
            this.SauditFormData.userName = this.userInfo.userName; //当前登录人名称
            if (this.$refs.publicForm0.submitForm() && this.currentDetail.fStatus == '待处理') {
                this.saveForm(this.closeFormData, 0);
            } else if (this.$refs.publicForm1.submitForm() && this.currentDetail.fStatus == '一级待审核') {
                this.saveForm(this.FauditFormData, 1);
            }else if (this.$refs.publicForm0.submitForm() && this.currentDetail.fStatus == '一级审核未通过') {
                this.saveForm(this.closeFormData, 0);
            }
            else if (this.$refs.publicForm1.submitForm() && this.currentDetail.fStatus == '二级审核未通过') {
                this.saveForm(this.FauditFormData, 1);
            }  else if (this.$refs.publicForm2.submitForm() && this.currentDetail.fStatus == '二级待审核') {
                this.saveForm(this.SauditFormData, 2);
            } else {
                console.log("error");
            }
            // if (this.$refs.publicForm0.submitForm()) {
            //     if(this.currentDetail.fStatus == '待处理'){
            //         this.saveForm(this.closeFormData,0);
            //     }else if(this.currentDetail.fStatus == '一级待审核'){
            //         this.saveForm(this.FauditFormData,1);
            //     }else if(this.currentDetail.fStatus == '二级待审核'){
            //         this.saveForm(this.SauditFormData,2);
            //     }

            // } else {
            //     console.log("error");
            // }
            // if (this.$refs.publicForm0.submitForm()) {
            //     if(this.currentDetail.fStatus == '待处理'){
            //         this.saveForm(this.closeFormData,0);
            //     }else if(this.currentDetail.fStatus == '一级待审核'){
            //         this.saveForm(this.FauditFormData,1);
            //     }else if(this.currentDetail.fStatus == '二级待审核'){
            //         this.saveForm(this.SauditFormData,2);
            //     }

            // } else {
            //     console.log("error");
            // }

        },
        // 保存
        saveForm(obj, status) {
            var url;
            this.loading = true;

            if (status == 0) {
                url = saveOutsourcednon //闭合
            } else if (status == 1) {
                url = checkOutsourcednon1 //一级审核
            } else if (status == 2) {
                url = checkOutsourcednon2 //二级审核
            }
            this.isLock = true
            url(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;

                        this.visible = false;
                        this.$router.push({
                            path: "/outsourcedManage/outUnQualifiedReports",
        
                        });
                        window.sessionStorage.removeItem("GxEmbedded_primaryInfo");

                        // this.$emit("refreshList");
                        // this.$message.success("新增成功");
                        this.$message.success({
                            content: "新增成功",
                            duration: 1000
                        })
                        this.isLock = false
                    } else {
                        this.loading = false;

                        this.isLock = false
                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        })
                        // this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })

        },
    },
};